package zhh.jdbc;

import java.sql.*;

public class DBTest {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/library_management?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String username = "root"; // 你的数据库用户名
        String password = "123456"; // 你的数据库密码

        String name="admin";
        String pwd="admin123";

        Connection conn=null;
        ResultSet rs=null;
        PreparedStatement prep =null;

        int id=0;
        String email=null;

        String sql="select * from user where username= ? and password = ?";

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);

            //查询结果
            prep = conn.prepareStatement(sql);
            prep.setString(1, name);
            prep.setString(2, pwd);
            //查询结果
            rs = prep.executeQuery();

            while (rs.next()) {
                id = rs.getInt("id");
                email = rs.getString("email");
            }

            prep.executeQuery();


            System.out.println(conn);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                prep.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

        System.out.println(id);
        System.out.println(email);


    }
}
